1427E - Xum - CodeForces Solution


bitmasks constructive algorithms math matrices number theory *2500

Please click on ads to support us..

Python Code:

from sys import stdin,stdout
input = stdin.readline

def main():
        t = 1
    for z in range(t):
        n = int(input())
                        n2 = n
        maxn = 10**18*5
        
        global ans2
        global ans
        ans = 0
        ans2 = []
        ans2 += [str(n) + " ^ " + str(n)]
                ans += 1
        for i in range(40):
                        ans2 += [str(n2) +" + " + str(n2)]
            ans += 1
            n2 <<= 1
            if n2*2 >= maxn:
                break
        num = bin(n)[2:]
        num2 = 1<<(len(num)-1)
        y = (num2 * n) ^ n
        ans2 += [str(num2 * n) +" ^ " + str(n)]
                ans += 1
        n2 = y
        for i in range(40):
                        ans2 += [str(n2) +" + " + str(n2)]
            ans += 1
            n2 <<= 1
            if n2*2 >= maxn:
                break
        def f(base,num):
            global ans2,ans
            number = 0
            res = 0
            while(num):
                if num & 1:
                                        ans2 += [str(res) +" + " + str(base * (1<<number))]
                    ans += 1
                    res += base * (1<<number)
                number += 1
                num >>= 1
        def gcdExtended(a, b):
            if a == 0:
                return b, 0, 1
         
            gcd, x1, y1 = gcdExtended(b % a, a)
            x = y1 - (b//a) * x1
            y = x1
         
            return gcd, x, y
                                              a = 0
        b = 0
        temp,a,b = gcdExtended(n, y)
        b = -b
        while a < 0 or b < 0:
            a,b = a+y,b+n
        if b % 2 != 0:
            a,b = a+y,b+n
                f(n,a)
        f(y,b)
                ans += 1
        print(ans)
        print("\n".join(ans2))
        print(a*n,"^",b*y)
main()


Comments

Submit
0 Comments
More Questions

Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people